Public Class CNguoiSuDung
    Private _user_id As Integer
    Private _loginname As String
    Private _pass As String
    Private _fullname As String
    Private _ban_id As Integer
    Private _ghichu As String
    Private _isAdmin As Boolean
    Private _isHide As Boolean

    Public Sub New()
        _isAdmin = False
        _isHide = False
        _ban_id = -1
    End Sub

#Region "Cac ham property"
    Public Property User_ID() As Integer
        Get
            Return _user_id
        End Get
        Set(ByVal Value As Integer)
            _user_id = Value
        End Set
    End Property

    Public Property LoginName() As String
        Get
            Return _loginname
        End Get
        Set(ByVal Value As String)
            _loginname = Value
        End Set
    End Property

    Public Property Pass() As String
        Get
            Return _pass
        End Get
        Set(ByVal Value As String)
            _pass = Value
        End Set
    End Property

    Public Property FullName() As String
        Get
            Return _fullname
        End Get
        Set(ByVal Value As String)
            _fullname = Value
        End Set
    End Property

    Public Property Ban_ID() As Integer
        Get
            Return _ban_id
        End Get
        Set(ByVal Value As Integer)
            _ban_id = Value
        End Set
    End Property

    Public Property GhiChu() As String
        Get
            Return _ghichu
        End Get
        Set(ByVal Value As String)
            _ghichu = Value
        End Set
    End Property

    Public Property IsAdmin() As Boolean
        Get
            Return _isAdmin
        End Get
        Set(ByVal value As Boolean)
            _isAdmin = value
        End Set
    End Property

    Public Property IsHide() As Boolean
        Get
            Return _isHide
        End Get
        Set(ByVal value As Boolean)
            _isHide = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return _loginname
    End Function
#End Region

    Public Function CapNhat(ByVal trangThaiAdminSau As Boolean) As Boolean
        Dim kq As Boolean = True
        Dim cauLenh As String = ""

        'thuc hien kiem tra trang thai Admin luc dau va trang thai Admin luc sau de cap nhat
        'If (_isAdmin = True And trangThaiAdminSau = False) Then
        '    'tuc la tu vi tri la Admin chuyen thanh khong phai la Admin
        '    cauLenh &= " exec sp_dropsrvrolemember '" & _loginname & "', 'sysadmin'"
        'End If
        'If (_isAdmin = False And trangThaiAdminSau = True) Then
        '    'duoc cap quyen Admin
        '    cauLenh &= " exec sp_addsrvrolemember '" & _loginname & "', 'sysadmin'"
        'End If

        'Dim strIsAdmin As String = "select IS_SRVROLEMEMBER('sysadmin','" + _loginname + "')"
        'If CInt(XL_DULIEU.LayGiaTri(strIsAdmin)) = 1 And trangThaiAdminSau = False Then
        '    cauLenh &= " exec sp_dropsrvrolemember '" & _loginname & "', 'sysadmin'"
        'ElseIf CInt(XL_DULIEU.LayGiaTri(strIsAdmin)) = 0 And trangThaiAdminSau = True Then
        '    cauLenh &= " exec sp_addsrvrolemember '" & _loginname & "', 'sysadmin'"
        'End If

        '_isAdmin = trangThaiAdminSau
        'thuc hien cau lenh cap nhat
        Dim strBanId As String = _ban_id.ToString()
        If (_ban_id = -1) Then
            strBanId = "null"
        End If
        cauLenh &= " update tblUser set"
        cauLenh &= " fullname =N'" & _fullname & "' "
        cauLenh &= ",ban_id =" & strBanId
        cauLenh &= ",ghichu = N'" & _ghichu & "'"
        If (trangThaiAdminSau = True) Then
            cauLenh &= ",isAdmin = 1"
        Else
            cauLenh &= ",isAdmin = 0"
        End If
        cauLenh &= " where user_id =" & _user_id.ToString()
        'thuc hien cau lenh
        If (XL_DULIEU.ThucHienLenh(cauLenh) = False) Then
            kq = False
        End If
        Return kq
    End Function

    Public Function themNSD() As Integer
        Dim kq As Integer
        kq = XL_DULIEU.ThemNSD(_loginname, _pass, _fullname, _ghichu)
        Return kq
    End Function

    Public Function XoaNSD() As Boolean
        'xoa trong co so du lieu
        Dim cauLenh As String = ""
        cauLenh &= "delete from tblUser where User_ID = " & _user_id
        If (XL_DULIEU.ThucHienLenh(cauLenh) = False) Then
            Return False
        End If
        Return True
    End Function

    'Public Shared Function XoaNSD(ByVal userName As String) As Boolean
    '    'xoa trong co so du lieu
    '    Dim cauLenh As String = ""

    '    cauLenh &= " delete from tblSYSVAR where user_id in (select user_id from tblUser where loginname ='" & userName & "')"
    '    cauLenh &= " delete from tblUser where loginname ='" & userName & "'"
    '    If (XL_DULIEU.ThucHienLenh(cauLenh) = False) Then
    '        Return False
    '    End If
    '    cauLenh = "exec sp_dropuser '" & userName & "'"
    '    If (XL_DULIEU.ThucHienLenh(cauLenh) = False) Then
    '        Return False
    '    Else
    '        Return True
    '    End If
    'End Function

End Class
